﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using codeGenerator.model;
using gudusoft.gsqlparser;
using gudusoft.gsqlparser.nodes;
using gudusoft.gsqlparser.pp.para;
using gudusoft.gsqlparser.pp.stmtformatter;

namespace codeGenerator
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            // DataObject.AddPastingHandler(SqlTextBox, TextBoxPlus_PastingEvent);
            this.DataContext = new AllData();
        }

        /**
         * 粘贴事件
         */
        private void TextBoxPlus_PastingEvent(object sender, DataObjectPastingEventArgs e)
        {
            var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
            if (!isText) return;
            var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
            // SqlTextBox.Text = formatSql(text);
        }

        private void EnterButton_OnClick(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(SqlTextBox.Text)) return;
            try
            {
                SqlTextBox.Text = FormatSql(SqlTextBox.Text);
                // 识别列成员
                string sqlStr = SqlTextBox.Text;
                GetTableColumns(sqlStr);
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message, "错误");
            }
        }

        private String FormatSql(String sqlText)
        {
            EDbVendor dbVendor = EDbVendor.dbvoracle;
            TGSqlParser parser = new TGSqlParser(dbVendor);

            parser.sqltext = sqlText;
            int ret = parser.parse();
            if (ret == 0)
            {
                GFmtOpt option = GFmtOptFactory.newInstance();
                return FormatterFactory.pp(parser, option);
            }
            else
            {
                MessageBox.Show(parser.Errormessage, "SQL错误");
                return sqlText;
            }
        }

        /**
         * 获取表字段
         */
        private void GetTableColumns(String sqlText)
        {
            TGSqlParser sqlParser = new TGSqlParser(EDbVendor.dbvoracle);
            sqlParser.sqltext = sqlText;
            sqlParser.parse();
            if (sqlParser.ErrorCount > 0) return;

            TCustomSqlStatement sqlStatement = sqlParser.sqlstatements.get(0);

            //TCreateTableSqlStatement createDatabaseSql = (TCreateTableSqlStatement) sqlParser.sqlstatements.get(0);
            //
            // foreach (TTable table in createDatabaseSql.tables)
            // {
            //     foreach (TObjectName tableLinkedColumn in table.LinkedColumns)
            //     {
            //         tableLinkedColumn.ty
            //     }
            // }

            TTableList tableList = sqlStatement.tables;
            List<TableProfile> tableProfiles = new List<TableProfile>();

            foreach (TTable table in tableList)
            {
                TableProfile tableProfile = new TableProfile();
                tableProfile.TableName = table.TableName.TableString;
                List<ColumnMeta> tableMetas = new List<ColumnMeta>();

                foreach (TObjectName column in table.LinkedColumns)
                {
                    ColumnMeta columnMeta = new ColumnMeta();
                    columnMeta.TableName = table.TableName.TableString;
                    columnMeta.ColumnName = column.ToString();

                    if (column.linkedColumnDefinition != null)
                    {
                        if (column.linkedColumnDefinition.Datatype != null)
                        {
                            columnMeta.ColumnType = column.linkedColumnDefinition.Datatype.DataTypeName;
                        }

                        // 备注待确认
                        if (column.linkedColumnDefinition.columnComment != null)
                        {
                            columnMeta.ColumnDesc = column.linkedColumnDefinition.columnComment.String;
                        }
                    }

                    tableMetas.Add(columnMeta);
                }

                tableProfile.TableMetas = tableMetas;
                tableProfiles.Add(tableProfile);
            }

            // 多数情况只有一张表
            string sql2JavaBean = tableProfiles[0].Sql2JavaBean();
            JavaTextBox.Text = sql2JavaBean;
        }

        private void tableNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
        }
    }

    public class AllData
    {
        private string _defaultTxt;

        public String defaultTxt
        {
            get => _defaultTxt;
            set => _defaultTxt = value;
        }

        public AllData()
        {
            this.defaultTxt =
                "";
        }
    }
}